<?php

namespace App\Admin\Extensions;

use Encore\Admin\Admin;

class Indicator
{
    protected $id;

    public function __construct($oid, $name, $auditor_id, $financial_id, $state = '')
    {
        $this->id = $oid;
        $this->auditor_id = $auditor_id;
        $this->financial_id = $financial_id;
        $this->state = $state;
    }

    protected function script()
    {
        return <<<SCRIPT

layui.use('layer', function(){
        var layer = layui.layer;
        $('.indicator').click(function () {
           var that = this;
           var id = $(that).attr('data-id');
           var index = layer.confirm('确定领取吗?',{title:'提示'} ,function(index){
                $.ajax({
                    url: '/admin/page/indicator?oid='+id,
                    type: 'get',
                    success: function(res){
                        alert(res.text);
                        layer.close(index);
                        location.href = location.href;
                        $(that).prop('disabled', true).css('backgroundColor', 'black');
                    }
                });
           });      
       });
       
        $('.auditallocation').click(function () {
             var that = this;
             var id = $(that).attr('data-id');
             var index = layer.open({
                content: '/admin/page/audit?oid='+id,
                type: 2,
                title: '审核',
                area: ['500px', '300px'],
                btn: ['确定', '取消'],
                yes: function(index, layero){
                    var user_status = $(layero).find("iframe").contents().find("select[name=user_status]").val();
                    var admin_id = $(layero).find("iframe").contents().find("select[name=admin_id]").val();
                  
                    $.ajax({
                        url: '/admin/page/audit?status='+user_status+'&oid='+id+'&admin_id='+admin_id,
                        type: 'post',
                        success: function (res) {
                            layer.msg(res.text)
                            if(res.code){
                                return;
                            }
                           
                            layer.close(index);
                            location.href = location.href
                        }
                    })
                }
            });
        });
});

SCRIPT;
    }

    protected function render()
    {
        Admin::script($this->script());
        if($this->auditor_id){
            if($this->financial_id || $this->state == -1){
                return "&nbsp;<a href='javascript:void(0);'><button data-id='{$this->id}' class='auditallocation layui-btn layui-btn-primary layui-btn-xs'>已审核</button></a>";
            }else{
                return "&nbsp;<a href='javascript:void(0);'><button data-id='{$this->id}' class='auditallocation layui-btn layui-bg-blue layui-btn-xs'>审核</button></a>";
            }
        }else{
            return "&nbsp;<a href='javascript:void(0);'><button data-id='{$this->id}' class='indicator layui-btn layui-btn-danger layui-btn-xs'>领取</button></a>";
        }
    }

    public function __toString()
    {
        return $this->render();
    }
}